package com.smartwebx.ha.web.admin.module.screen.report;

import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwebx.ha.web.admin.common.util.HaTool;
/**
 * 
 * 创建人：Dream~天涯 
 * 创建时间：2015-10-22 下午1:43:29
 * 邮箱：154494796@qq.com
 * 备注：引排水站列表
 */
public class StStbprpBWdpstat {
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private HttpServletRequest request;
	public void execute() throws Exception{
		ConnUtils connUtils = new ConnUtils();
		response.setContentType("application/json");
		PrintWriter out=response.getWriter();
		ObjectMapper mapper=new ObjectMapper();
		
		Cookie[] cookies = request.getCookies();
		String stStbprpBc = HaTool.getCookie(request,cookies,"stStbprpBWdpstat");
		String stStbprpB = null;
		String isstStbprpB = getStStbprpB();
		if(stStbprpBc==null||"".equals(stStbprpBc)||isstStbprpB.length()<8){
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			conn = connUtils.getConnection();
			ps = conn.prepareStatement("select C.STCD,C.STNM from jssq.ST_WDPSTAT_R B inner join jssq.ST_STBPRP_B C ON B.STCD = C.STCD group by C.STCD,C.STNM");		
			rs = ps.executeQuery();
			StringBuffer sb = new StringBuffer();
			while( rs.next()){
				sb.append(rs.getString("STNM"));
				sb.append("[");
				sb.append(rs.getString("STCD"));
				sb.append("],");
			}
			stStbprpB = sb.toString().replace(" ", "");
			if(stStbprpB.length()>10){
				Cookie stStbprpBCookie = new Cookie("stStbprpBWdpstat", "true");
				stStbprpBCookie.setMaxAge(3600 * 24);
				response.addCookie(stStbprpBCookie);
				File datafile = new File(this.getClass().getResource("").getPath()+"stStbprpBWdpstat.txt");
				PrintWriter pw = new PrintWriter(datafile);
				pw.write(stStbprpB);
				pw.close();
			}else{
				stStbprpB = getStStbprpB();
			}
		}else if("true".equals(stStbprpBc)){
			stStbprpB = getStStbprpB();
		}
		mapper.writeValue(out, stStbprpB);
	}
	
	public String getStStbprpB() throws Exception{
		File file = new File(this.getClass().getResource("").getPath()+"stStbprpBWdpstat.txt");
        FileReader reader = new FileReader(file);
        int fileLen = (int)file.length();
        char[] chars = new char[fileLen];
        reader.read(chars);
        String stStbprpB = String.valueOf(chars);
        reader.close();
		return stStbprpB;
	}
}
